package scenic.toolkit.db;

public class NodeArray<T extends Node> extends Array {
	private Node[] data;

	public NodeArray(Type elementType) {
		super(new ArrayType(elementType, 0));
	}
	
	protected Object getData() {
		return data;
	}
	
	protected void setData(Object data) {
		this.data = (Node[])data;
	}
	
	protected Object allocate(int size) {
		return new Node[size];
	}
	
	public void write(int position, T[] insertData) {
		Node[] objects = new Node[insertData.length];
		
		for(int i = 0; i < objects.length; i++)
			objects[i] = insertData[i];
		super.write(position, objects, insertData.length);
	}
	
	public void insert(int position, T[] insertData) {
		Node[] objects = new Node[insertData.length];
		
		for(int i = 0; i < objects.length; i++)
			objects[i] = insertData[i];
		super.insert(position, objects, insertData.length);
	}
	
	public void set(int index, T value) {
		if(index >= getLength())
			throw new IndexOutOfBoundsException();
		data[index] = value;
		super.notifyWrite(index, 1);
	}
	
	@SuppressWarnings("unchecked")
	public T get(int index) {
		if(index >= getLength())
			throw new IndexOutOfBoundsException();
		return (T)data[index];
	}
	
	public void storeTo(StorageOutputStream os) {
		os.write(this);
	}	
}
